﻿using System;
using System.Collections.Generic;
using System.Linq;
using Web7.Extensions;
using Web7.Core.Security.Users;
using System.Security.Principal;
using Web7.Core.Security;
using Web7.Domain;
using Web7.Utilities;

namespace Web7.Container.Models
{
    public class User : Entity, IUser
    {
        public const string EmailPattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";

        public User() { }
        //public User(string username, string email, string password, UserStatus status, UserGroupBasic userGroup)
        //{
        //    Identity = new UserIdentity(null, false, username);
        //    Username = username;
        //    UserGroup = userGroup;
        //    Email = email;
        //    Password = password;
        //    Status = status;
        //    AuthenticationValues = new Dictionary<string, object>();

        //    Roles = Enumerable.Empty<RoleBasic>();
        //    UserGroupRoles = Enumerable.Empty<UserGroupBasic>();
        //    UserGroupRolePermissions = Enumerable.Empty<PermissionBasic>();
        //    RolePermissions = Enumerable.Empty<PermissionBasic>();
        //    Permissions = Enumerable.Empty<PermissionBasic>();
        //}
        //public User(int userID, string username, string email, string password, UserStatus status, UserGroupBasic userGroup)
        //    : this(username, email, password, status, userGroup)
        //{
        //    Identity = new UserIdentity(null, true, username);
        //    Id = userID;
        //}

        //public User(int userID, string username, string email, string password, UserStatus status, UserGroupBasic userGroup, DateTime creationDate
        //    , IEnumerable<UserGroupBasic> userGroupRoles
        //    , IEnumerable<RoleBasic> userRoles
        //    , IEnumerable<PermissionBasic> userGroupRolePermissions
        //    , IEnumerable<PermissionBasic> rolePermissions
        //    , IEnumerable<PermissionBasic> userPermissions)
        //    : this(userID, username, email, password, status, userGroup)
        //{
        //    CreationDate = creationDate;
        //    Roles = userRoles;
        //    UserGroupRoles = userGroupRoles;
        //    UserGroupRolePermissions = userGroupRolePermissions;
        //    RolePermissions = rolePermissions;
        //    Permissions = userPermissions;
        //}

        public virtual string UserName { get; set; }

        public virtual string Email { get; set; }

        public virtual string Password { get; set; }

        public virtual SymmetricEncryptType PasswordFormat { get; set; }

        public virtual UserStatus Status { get; set; }

        public virtual UserStatus EmailStatus { get; set; }

        public virtual DateTime CreationDate { get; set; }

        public virtual bool IsAuthenticated { get; set; }

        public virtual T Cast<T>() where T : class, IUser
        {
            return this as T;
        }

        ///// <summary>
        ///// 用户拥有的特定角色
        ///// </summary>        
        //public virtual IEnumerable<RoleBasic> Roles { get; set; }
        ///// <summary>
        ///// 用户所属用户组所拥有的角色
        ///// </summary>
        //public virtual IEnumerable<UserGroupBasic> UserGroupRoles { get; set; }
        ///// <summary>
        ///// 用户拥有的特定权限
        ///// </summary>
        //public virtual IEnumerable<PermissionBasic> Permissions { get; set; }
        ///// <summary>
        ///// 用户所属用户组所拥有的角色所拥有的权限
        ///// </summary>
        //public virtual IEnumerable<PermissionBasic> UserGroupRolePermissions { get; set; }
        ///// <summary>
        ///// 用户拥有的角色所拥有的权限
        ///// </summary>
        //public virtual IEnumerable<PermissionBasic> RolePermissions { get; set; }
        //#region IUser Members

        //public virtual string Emal { get { return Identity.Name; } }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userGroupName"></param>
        /// <returns></returns>
        //public virtual bool IsInUserGroup(string userGroupName)
        //{
        //    if (String.IsNullOrEmpty(userGroupName)) return true;
        //    return String.Compare(UserGroup.UserGroupName, userGroupName, true/*ignoreCase*/) == 0;
        //}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        //public virtual bool HasPermission(string permissionName)
        //{
        //    return hasPermission(permissionName, Permissions)
        //        || hasPermission(permissionName, UserGroupRolePermissions)
        //        || hasPermission(permissionName, RolePermissions);
        //}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="permissionID"></param>
        /// <returns></returns>
        //public virtual bool HasPermission(Guid permissionID)
        //{
        //    return hasPermission(permissionID, Permissions)
        //        || hasPermission(permissionID, UserGroupRolePermissions)
        //        || hasPermission(permissionID, RolePermissions);
        //}
        //#endregion
        //#region IPrincipal Members
        //public virtual IIdentity Identity { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        //public virtual bool IsInRole(string role)
        //{
        //    return isInRole(role, Roles) || isInRoleGroup(role, UserGroupRoles);
        //}
        //#endregion
        //#region Private Static Methods
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="permissionName"></param>
        ///// <param name="permissions"></param>
        ///// <returns></returns>
        ////private static bool hasPermission(string permissionName, IEnumerable<PermissionBasic> permissions)
        ////{
        ////    if (String.IsNullOrEmpty(permissionName)) return true;
        ////    if (permissions == null || permissions.Count() == 0) return false;
        ////    foreach (PermissionBasic permission in permissions)
        ////        if (string.Compare(permission.PermissionName, permissionName, true) == 0)
        ////            return true;
        ////    return false;
        ////}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="permissionID"></param>
        ///// <param name="permissions"></param>
        ///// <returns></returns>
        ////private static bool hasPermission(Guid permissionID, IEnumerable<PermissionBasic> permissions)
        ////{
        ////    if (permissionID == Guid.Empty) return true;
        ////    if (permissions == null || permissions.Count() == 0) return false;
        ////    foreach (PermissionBasic permission in permissions)
        ////        if (permission.PermissionID == permissionID)
        ////            return true;
        ////    return false;
        ////}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="roleName"></param>
        ///// <param name="roles"></param>
        ///// <returns></returns>
        ////private static bool isInRole(string roleName, IEnumerable<RoleBasic> roles)
        ////{
        ////    if (roleName.IsNullOrEmpty()) return true;
        ////    if (roles.IsNullOrEmpty()) return false;
        ////    foreach (RoleBasic role in roles)
        ////        if (string.Compare(role.RoleName, roleName, true) == 0)
        ////            return true;
        ////    return false;
        ////}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="roleName"></param>
        ///// <param name="groups"></param>
        ///// <returns></returns>
        ////private static bool isInRoleGroup(string roleName, IEnumerable<UserGroupBasic> groups)
        ////{
        ////    if (roleName.IsNullOrEmpty()) return true;
        ////    if (groups.IsNullOrEmpty()) return false;
        ////    foreach (UserGroupBasic group in groups)
        ////        foreach (Role role in group.Roles)
        ////            if (string.Compare(role.RoleName, roleName, true) == 0)
        ////                return true;
        ////    return false;
        ////}
        //#endregion
    }
}
